1486A - Shifting Stacks - CodeForces Solution


greedy implementation *900

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    curr_sum = 0
    is_possible = True
    for i, x in enumerate(a):
        curr_sum += x
        if 2 * curr_sum < i * (i + 1):
            is_possible = False
            break
    if is_possible:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
#include <iomanip>
#include <climits>

using namespace std;

#define ll long long int
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vch vector<char>
#define vvch vector<vector<char>>
#define vvll vector<vector<ll>>
#define mod 1000000007

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

void rvec(vll &arr, ll n)
{
    for (ll i = 0; i < n; i++)
        cin >> arr[i];
}

void pvec(vll &arr, ll n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

ll power(ll a, ll b)
{
    if (b == 0)
        return 1;
    if (b == 1)
        return a;
    if (b % 2 == 0)
        return power(a, b / 2) * power(a, b / 2);
    else
        return a * power(a, b / 2) * power(a, b / 2);
}
unsigned ll factorial(unsigned ll n)
{
    if (n == 0 || n == 1)
        return 1;
    return (n * factorial(n - 1)) % mod;
}

int main()
{

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);

    ll t = 1;
    cin >> t;

    while (t--)
    {
        ll n, val = 0;
        bool flag = false;
        cin >> n;
        vll arr(n);
        rvec(arr, n);
        ll need = 0, sum = 0;
        // TRY TO MAKE ARRAY LIKE 0,1,2,3,4,5....
        // THUS FOR EVERY INDEX THE SUM OF BLOCKS TILL THEN SHOULD BE GREATER THAN WE IDEAL WANT
        for (ll i = 0; i < n; i++)
        {
            need += i;
            sum += arr[i];
            if (sum < need)
            {
                flag = true;
                cout << "NO\n";
                break;
            }
        }
        if (!flag)
            cout << "YES\n";
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction
159C - String Manipulation 10
258A - Little Elephant and Bits
1536C - Diluc and Kaeya
1428C - ABBB
1557A - Ezzat and Two Subsequences
255A - Greg's Workout
1059A - Cashier